<?php
class Menu_db extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function get_menu_groups($limit = '', $offset = '') {
        $this->db->order_by("mgposition", "asc");
        $this->db->order_by("mglabel", "asc");
        if($limit != '' && $offset != '')
            $this->db->limit($limit,$offset); 
        $query = $this->db->get('menugroup');
        $result = $query->result_array();
        return $result;    
    }
    
    function get_menu_groups_list($limit = '', $offset = '') {
        $this->db->order_by("mgposition", "asc");
        $this->db->order_by("mglabel", "asc");
        if($limit != '' && $offset != '')
            $this->db->limit($limit,$offset); 
        $query = $this->db->get('menugroup');
        $result = $query->result_array();
        $listresult = array();
        foreach( $result as $resultitem  ) {
            array_push($listresult, array( 'label' => $resultitem['mglabel'], 'value' => $resultitem['mgid'] ));
        }
        return $listresult;    
    }
    
    function get_user_roles($data=NULL) {
        if(isset($data))
        $this->db->where($data);
        $query = $this->db->get('roles');
        $result = $query->result_array();
        return $result; 
    }
    
    function get_mi_roles($data=null) {
        $this->db->where($data);
        $query = $this->db->get('roleaccess');
        $result = $query->result_array();
        $rs = array();
        foreach($result as $row) {
            if($row['allowed'] == 1)
                array_push($rs,$row['roleid']);
        }
        return $rs;
    }
    
    function build_mg_page_data( $pageno = 1 ){
        $this->db->from('menugroup');
        $result['dg_title'] = "Menu Groups";
        $result['total'] = $this->db->count_all_results();        
        $result['current_page'] = $pageno;
        $limit = $this->config->item('per_page');
        $result['rows_per_page'] = $limit;
        $result['pages_total'] = ceil($result['total'] / $limit);
        $result['pk'] = 'mgid';
        $result['itemname'] = 'mglabel';
        $result['dgcolumns'] = array(
            'mgid'=>array('label'=>'Group Id','type'=>'number','visible'=>false),
            'mglabel'=>array('label'=>'Label','type'=>'string','visible'=>true),
            'mgposition'=>array('label'=>'Position','type'=>'number','visible'=>true),
            'mgurl'=>array('label'=>'Url','type'=>'string','visible'=>true)
        );
        $result['actions'] = array(
        'add'=>'menu/addmenugroup',
        'edit'=>'menu/editmenugroup',
        'delete'=>'menu/deletemenugroup',
        'view'=>'menu/viewmenugroup',
        'page'=>'menu/viewmenuitem'
        );
                
        $offset = $limit*($pageno-1);
        $result["dgitems"] = $this->get_menu_groups($limit, $offset);
        return $result;
    }
    
    function get_mi_list($limit=0,$start=0,$filterdata=null) {
        $this->db->from('menuitem mi');
        $this->db->join('menugroup mg','mi.mgid = mg.mgid','left');        
        $this->db->order_by("mg.mgposition", "asc");
        $this->db->order_by("mg.mgid", "asc");
        $this->db->order_by("mi.miposition", "asc");
        $this->db->limit($limit, $start); 
        $query = $this->db->get();
        //print_object($this->db->last_query());
        return $query->result_array();
    }
    
    function get_mi_filter_count($filterdata='') {
        $this->db->from('menuitem mi');
        $this->db->join('menugroup mg','mi.mgid = mg.mgid','left');
        
        if($filterdata!='') {
            $fitems = $filterdata['items'];
            foreach($fitems as $fitem) {
                $tbl = $fitem['table'];
                $field = $fitem['name'];
                $value = $fitem['value'];
                if( $value != '') {
                    if(is_array($value)) {
                        $this->db->where_in($tbl.'.'.$field,$value);
                    } else {
                        if($fitem['type']=='text')
                            $this->db->like($tbl.'.'.$field,$value);
                        else
                            $this->db->where($tbl.'.'.$field,$value);
                    }
                }
            }
        }
        
        return $this->db->count_all_results();
    }
    
    function get_mi_filter_list($limit=0,$start=0,$filterdata='') {
        $this->db->from('menuitem mi');
        $this->db->join('menugroup mg','mi.mgid = mg.mgid','left');
        
        if($filterdata!='') {
            $fitems = $filterdata['items'];
            foreach($fitems as $fitem) {
                $tbl = $fitem['table'];
                $field = $fitem['name'];
                $value = $fitem['value'];
                if( $value != '') {
                    if(is_array($value)) {
                        $this->db->where_in($tbl.'.'.$field,$value);
                    } else {
                        if($fitem['type']=='text')
                            $this->db->like($tbl.'.'.$field,$value);
                        else
                            $this->db->where($tbl.'.'.$field,$value);
                    }
                }
            }
        }
                
        $this->db->order_by("mg.mgposition", "asc");
        $this->db->order_by("mg.mgid", "asc");
        $this->db->order_by("mi.miposition", "asc");
        $this->db->limit($limit, $start); 
        $query = $this->db->get();
        //print_object($this->db->last_query());
        return $query->result_array();
    }
    
    function build_mi_dg_data($fiterdata='') {
        $result['dg_title'] = "Menu Items";
        if($this->input->post('show_all')) {
            $result['show_all'] = true;
        } else {
            //$result['show_all'] = false;
            $result['current_page'] = ($this->input->post('page')) ? $this->input->post('page') : 1;
            $result['rows_per_page'] = ($this->config->item('per_page')) ? $this->config->item('per_page') : 10;
        }
        
        if($fiterdata=='')
            $result['total'] = $this->db->count_all_results('menuitem');
        else
            $result['total'] = $this->get_mi_filter_count($fiterdata);
            
        if($result['show_all']) {
            $result['current_page'] = 1;
            $result['rows_per_page'] = ($result['total']==0)?100:$result['total'];
        }
        
        $result['pk'] = 'miid';
        $result['itemname'] = 'milabel';
        $result['dgfilter'] = false;
        $result['dgcolumns'] = array(
            'mgid'=>array('label'=>'Group Id','type'=>'number','visible'=>false),
            'miid'=>array('label'=>'Menu Id','type'=>'number','visible'=>false),
            'mglabel'=>array('label'=>'Menu Group','type'=>'string','visible'=>true,'filter'=>true),
            'milabel'=>array('label'=>'Menu Label','type'=>'link','visible'=>true,'href'=>'menu/viewmenuitem'),
            'miposition'=>array('label'=>'Position','type'=>'number','visible'=>true,'filter'=>true),
            'mgurl'=>array('label'=>'Group Url','type'=>'string','visible'=>true,'filter'=>true),
            'miurl'=>array('label'=>'Menu Url','type'=>'string','visible'=>true,'filter'=>true),
            'miactive'=>array('label'=>'Active','type'=>'boolean','visible'=>true,'filter'=>true),
            'mihidden'=>array('label'=>'Hidden','type'=>'boolean','visible'=>true,'filter'=>true)            
        );
        
        $result['actions'] = array(
        'add'=>'menu/addmenuitem',
        'edit'=>'menu/editmenuitem',
        'delete'=>'menu/deletemenuitem',
        'view'=>'menu/viewmenuitem',
        'page'=>'menu/searchmenuitem'
        );
        
        $offset = $result['rows_per_page']*($result['current_page']-1);
        $result['pages_total'] =  ceil($result['total']/$result['rows_per_page']);
        if($fiterdata=='')
            $result["dgitems"] = $this->get_mi_list($result['rows_per_page'], $offset);
        else
            $result["dgitems"] = $this->get_mi_filter_list($result['rows_per_page'], $offset, $fiterdata);
        return $result;
    }
    
    function save_mg($data,$pk='') {
        $save_result = array();
        
        if ($pk=='') {
    		$this->db->insert('menugroup', $data);
    		$save_result['insert_id'] = $this->db->insert_id();
            if($save_result['insert_id'] == 0 || $save_result['insert_id'] == false) {
                $save_result['result'] = 'F';
                $save_result['error_no'] = $this->db->_error_number();
                $save_result['error_message'] = $this->db->_error_message();
            } else {
                $save_result['result'] = 'S';
            }
		} else {
    		$this->db->where('mgid', $pk);
    		$this->db->update('menugroup', $data);
            //print_object($this->db->last_query());exit;
            $save_result['affected_rows'] = $this->db->affected_rows();
            if($save_result['affected_rows'] == -1 ) {
                $save_result['result'] = 'F';
                $save_result['error_no'] = $this->db->_error_number();
                $save_result['error_message'] = $this->db->_error_message();
            } else {
                $save_result['result'] = 'S';
            }
		}   
        return $save_result;
    }
    
    function is_mg_duplicate($url) {
        $this->db->where('mgurl', $url);
        $query = $this->db->get('menugroup');
        $rowcount = $query->num_rows();
        if($rowcount > 0)
            return true;
        else
            return false;
    }
    
    function is_mi_duplicate( $mgid, $miurl ) {
        $this->db->where( array('mgid' => $mgid, 'miurl' => $miurl) );
        $query = $this->db->get('menuitem');
        $rowcount = $query->num_rows();
        if($rowcount > 0)
            return true;
        else
            return false;
    }
    
    function save_mi($data,$pk='') {
        $save_result = array();
        
        if ($pk=='') {
    		$this->db->insert('menuitem', $data);
    		$save_result['insert_id'] = $this->db->insert_id();
            if($save_result['insert_id'] == 0 || $save_result['insert_id'] == false) {
                $save_result['result'] = 'F';
                $save_result['error_no'] = $this->db->_error_number();
                $save_result['error_message'] = $this->db->_error_message();
            } else {
                $save_result['result'] = 'S';
            }
		} else {
    		$this->db->where('miid', $pk);
    		$this->db->update('menuitem', $data);
            //print_object($this->db->last_query());exit;
            $save_result['affected_rows'] = $this->db->affected_rows();
            if($save_result['affected_rows'] == -1 ) {
                $save_result['result'] = 'F';
                $save_result['error_no'] = $this->db->_error_number();
                $save_result['error_message'] = $this->db->_error_message();
            } else {
                $save_result['result'] = 'S';
            }
		}   
        return $save_result;
    }
    
    function get_mg($data) {
        $this->db->where($data);
        $query = $this->db->get('menugroup');
        $result = $query->result_array();
        return $result; 
    }
    
    function get_mi($data) {
        $this->db->from('menuitem mi');
        $this->db->join('menugroup mg','mi.mgid = mg.mgid','left');
        $this->db->where($data);
        $query = $this->db->get();
        $result = $query->result_array();
        return $result; 
    }
    
    function delete_mg($data) {
        $this->db->where($data);
        $result = $this->db->delete('menugroup');
        //print_object($result);exit;
        //print_object($this->db->last_query());exit;
        if ($this->db->affected_rows() > 0)
            return TRUE;
        return FALSE;
    }
    
    function delete_mi($data) {
        $this->db->where($data);
        $result = $this->db->delete('menuitem');
        //print_object($result);exit;
        //print_object($this->db->last_query());exit;
        if ($this->db->affected_rows() > 0)
            return TRUE;
        return FALSE;
    }
    
    function save_mi_access($data, $miid = '') {
        //print_object($data); exit;
        if($miid!='')
        $this->db->delete('roleaccess', array('miid' => $miid));
        $result = $this->db->insert_batch('roleaccess', $data);
        if ($this->db->affected_rows() > 0)
            return TRUE;
        return FALSE;
    }
}
?>